package com.hzlj.position.dbsync.liaoning.controller.notify;

import cn.hutool.extra.servlet.ServletUtil;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.enums.CoordinateType;
import com.fz.common.base.exception.PubException;
import com.hzlj.position.config.common.dto.common.PositionAddDTO;
import com.hzlj.position.config.common.enums.WarningType;
import com.hzlj.position.dbsync.common.config.PositionDbSyncConfig;
import com.hzlj.position.dbsync.liaoning.common.dto.zhizunWd.request.ZhizunWdActionNotifyDTO;
import com.hzlj.position.dbsync.liaoning.common.dto.zhizunWd.request.ZhizunWdBaseNotifyDTO;
import com.hzlj.position.dbsync.liaoning.common.dto.zhizunWd.request.ZhizunWdLocationNotifyDTO;
import com.hzlj.position.dbsync.liaoning.common.dto.zhizunWd.response.ZhizunWdDeviceLocationQueryResultDTO;
import com.hzlj.position.dbsync.liaoning.rest.ZhizunRest;
import com.hzlj.position.locate.common.dto.position.PositionExtDTO;
import com.hzlj.position.locate.common.util.PositionRestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/position/locate/wd/notify/zhizun")
public class ZhizunWdNotifyController {

    @Resource
    private PositionDbSyncConfig positionDbSyncConfig;
    @Resource
    private ZhizunRest zhizunRest;

    /**
     * 通知
     */
    @PostMapping("")
    public String notify(HttpServletRequest request) {
        String body = ServletUtil.getBody(request);
        log.info("【智尊】收到数据推送：body={}", body);
        ZhizunWdBaseNotifyDTO notify = JsonUtils.json2obj(body, ZhizunWdBaseNotifyDTO.class);
        //定位推送
        try {
            //定位
            if (notify.isLocation() ) {
                PositionAddDTO position = JsonUtils.json2obj(body, ZhizunWdLocationNotifyDTO.class).convert(WarningType.SUCCESS);
                PositionRestUtil.add(CoordinateType.GCJ02,positionDbSyncConfig.getUrl(), position);
            }
            //拆卸
            if (notify.isDemount()) {
                log.info("【智尊】发生拆卸现象,需要告警");
                PositionAddDTO position = JsonUtils.json2obj(body, ZhizunWdActionNotifyDTO.class).convert(WarningType.DEMOUNT);
                PositionRestUtil.add(CoordinateType.GCJ02,positionDbSyncConfig.getUrl(), position);
            }
            //摘除
            if (notify.isRemove()) {
                log.info("【智尊】发生摘除现象,需要告警");
                PositionAddDTO position = JsonUtils.json2obj(body, ZhizunWdActionNotifyDTO.class).convert(WarningType.DEMOUNT);
                PositionRestUtil.add(CoordinateType.GCJ02,positionDbSyncConfig.getUrl(), position);
            }
            //低电
            if(notify.isLowbat()){
                log.info("【智尊】发生低电现象,需要告警");
                PositionAddDTO position = JsonUtils.json2obj(body, ZhizunWdActionNotifyDTO.class).convert(WarningType.DEMOUNT);
                ZhizunWdDeviceLocationQueryResultDTO deviceLocation = zhizunRest.getDeviceLocation(position.getDeviceCode());
                position.setPositionExt(
                        new PositionExtDTO().setElectricValue(deviceLocation.getB())
                );
                PositionRestUtil.add(CoordinateType.GCJ02,positionDbSyncConfig.getUrl(), position);
            }
        } catch (PubException e) {
            return "fail";
        }
        return "ok";
    }
}
